
import java.math.BigDecimal;
import java.util.Date;

import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bookstore.model.entity.Book;
import com.bookstore.model.entity.Chapter;
import com.bookstore.model.util.HibernateUtil;

public class Main {
	
	public static void main(String[] args) {
		
		Session session = HibernateUtil.getSession();		
		Transaction tx = session.beginTransaction();
		session.setCacheMode(CacheMode.IGNORE);
		
		for (int i = 0; i < 10000; i++) {
						
			Book book = new Book();
			book.setName("Book" + i);
			book.setIsbn("ISBN" + i);
			book.setPrice(new BigDecimal(10.99));
			book.setPublishDate(new Date());		
			
			Chapter chapter1 = new Chapter();
			chapter1.setTitle("Chapter One");
			chapter1.setNumOfPages(10);
			book.getChapters().add(chapter1);
			
			Chapter chapter2 = new Chapter();
			chapter2.setTitle("Chapter Two");
			chapter2.setNumOfPages(10);			
			book.getChapters().add(chapter2);
			
			session.save(book);	
			session.save(chapter1);
			session.save(chapter2);
			
		/*	if (i % 1000 == 0){
				System.out.println("flushing in batches");
				session.flush();
				session.clear();
			}*/
			
		}
		tx.commit();
		session.close();		
		
	}

}
